﻿using System;

namespace RaspberryDebugger
{
    /// <summary>
    /// 用于报告连接问题。
    /// </summary>
    internal class ConnectionException : Exception
    {
        //---------------------------------------------------------------------
        // Static members

        /// <summary>
        /// 将连接和错误消息转换为异常消息。  
        /// </summary>
        /// <param name="connection">The offending connection.</param>
        /// <param name="error">The error message.</param>
        /// <returns>The exception message.</returns>
        private static string GetMessage(Connection connection, string error)
        {
            var name = connection?.Name ?? "????";
            
            error = error ?? "unspecified error";

            return $"[{name}]: {error}";
        }

        /// <summary>
        ///将连接名称和错误消息转换为异常消息。  
        /// </summary>
        /// <param name="name">The offending connection name.</param>
        /// <param name="error">The error message.</param>
        /// <returns>The exception message.</returns>
        private static string GetMessage(string name, string error)
        {
            name  = name ?? "????";
            error = error ?? "unspecified error";

            return $"[{name}]: {error}";
        }

        //---------------------------------------------------------------------
        // Instance members

        /// <summary>
        ///构造一个基于<see cref="Connection"/>和  
        /// 一条错误消息。
        /// </summary>
        /// <param name="connection">The offending connection.</param>
        /// <param name="error">The error message.</param>
        public ConnectionException(Connection connection, string error)
            : base(GetMessage(connection, error))
        {
        }

        /// <summary>
        /// 根据连接名称和构造一个实例  
        /// error message.
        /// </summary>
        /// <param name="name">The offending connection name.</param>
        /// <param name="error">The error message.</param>
        public ConnectionException(string name, string error)
            : base(GetMessage(name,error))
        {
        }
    }
}
